<?php

namespace App\View\Components;

use App\Models\Admin;
use App\Models\Permissions;
use App\Services\ModuleService;
use Illuminate\Http\Request;
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\DB;
use Illuminate\View\Component;
use App\Models\Roles;

class AdminSidebarX extends Component
{
    public $page_name = '';
    public $base_path = '';
    public $path = '';
    private $moduleService;
    /**
     * Create a new component instance.
     *
     * @return void
     */
    public function __construct(Request $request, $page_name = '', ModuleService $moduleService)
    {
        $this->page_name = $page_name;
        $this->se = session()->get('account');
        $this->getPageName($request);
        $this->moduleService = new ModuleService();
    }

    /**
     * Get the view / contents that represent the component.
     *
     * @return \Illuminate\View\View|string
     */
    public function render()
    {
        $role_id = session('admin.role_id') ?? 0;
        $user_type = session('admin.user_type');
        if (empty($role_id)) {
            $menu = [];
        } else {
            $menu = $this->moduleService->getRoleMenu($role_id);
        }

        return view('components.admin-sidebar-x', ['menu' => $menu]);
    }

    /**
     * 获取老旧菜单
     *
     * @param int $user_type
     * @return array
     */
    public function getOldMenu(int $user_type)
    {
        if ($user_type == 1) {
            $menu = $this->getMenu();
        }
        if ($user_type == 2) {
            $menu = $this->getDistributorMenu();
        }
        if ($user_type >= 4) {
            $menu = $this->getMenuByRole();
        }

        return $menu ?? [];
    }

    /*
     * 根据用户角色（user_type）获取导航*/
    public function getMenuByRole()
    {
        $admin_id = session('admin.id');
        $user_type = session('admin.user_type');
        $role = Roles::find($user_type);
        $per = Permissions::get(['id', DB::raw('simplified_chiness_value as text'), 'url', 'page_name', 'icon', 'parent_id']);
        $ps = $role->permissions()->get(['id']);
        $pids = Arr::pluck($ps, 'id');
        $menu = [];
        $m1 = [];
        foreach ($per as $k => $v) {
            $m1[$v->id] = [
                'icon' => $v->icon,
                'text' => $v->text,
                'url' => $v->url,
                'page_name' => [$v->page_name],
            ];
        }

        foreach ($per as $k => $v) {
            if ($v->parent_id >= 1) {
                if (in_array($v->id, $pids)) {
                    $menu[] = [
                        'page_name' => [$v->page_name],
                        'url' => $v->url,
                        'text' => $v->text,
                    ];
                }
            }
            /*if (in_array($v->id, $pids)) {
                if (empty($v->parent_id)) {
                    if (empty($menu[$v->id])) {
                        $menu[$v->id] = @$m1[$v->id];
                    }
                } else {
                    if (empty($menu[$v->parent_id])) {
                        $menu[$v->parent_id] = @$m1[$v->parent_id];
                    }
                    $menu[$v->parent_id]['sub'][] = $m1[$v->id];
                    $menu[$v->parent_id]['page_name'] = array_merge($menu[$v->parent_id]['page_name'], $m1[$v->id]['page_name']);
                }
            }*/
        }
        return $this->makeUrlItem($menu);
    }

    protected function getPageName(Request $request)
    {
        if (!empty($this->page_name)) {
            return ;
        }
        $path = $request->path();
        $prefix = 'admin/';
        $path = substr($path, mb_strlen($prefix)); // online

        $base_path = [
            'distributor',
            'dealer',
            'customer',
            'rolls',
            'precut',
            'setting',
            'logs',
            'warranty',
            'charts',
            'distributor-o',
            'dealer-o',
            'rolls-transfer',
            'rolls-return',
            'roles',
            'tables',
            'new-precut',
        ];
        $this->base_path = $base_path;
        $this->path = $path;
        if (empty($path)) {
            $this->page_name = 'index';
        }
        foreach ($base_path as $k => $v) {
            if (strpos($path,$v.'/')===0 || $path==$v) {
                $this->page_name = $v;
            }
        }
        //  || $path == 'rolls-transfer'
        if (strpos($path, 'rolls-transfer/create') === 0) {
            $this->page_name = 'rolls-transfer/create';
        }
        if (strpos($path, 'rolls/importDistribution') === 0) {
            $this->page_name = 'rolls/importDistribution';
        }

        if (strpos($path, 'new-precut-transfer') === 0) {
            $this->page_name = 'new-precut-transfer';
        }

        if (strpos($path, 'new-precut-transfer/check') === 0) {
            $this->page_name = 'new-precut-transfer-check';
        }

        if (strpos($path, 'new-precut-transfer/create') === 0) {
            $this->page_name = 'new-precut-transfer-create';
        }

        //  || $path == 'rolls-return'
        if (strpos($path, 'rolls-return/create') === 0) {
            $this->page_name = 'rolls-return/create';
        }
        if (strpos($path, 'index') === 0) {
            $this->page_name = 'index';
        }

        if (strpos($path, 'verify/transfer/') === 0 || $path == 'verify/transfer') {
            $this->page_name = 'verify/transfer';
        }
        if (strpos($path, 'verify/warranty/') === 0 || $path == 'verify/warranty') {
            $this->page_name = 'verify_warranty';
        }
        if (strpos($path, 'precut_deal/') === 0 || $path == 'precut_deal') {
            $this->page_name = 'precut_deal';
        }
        if (strpos($path, 'dblogs/') === 0 || $path == 'dblogs') {
            $this->page_name = 'dblogs';
        }
        if (strpos($path, 'user_sync/') === 0 || $path == 'user_sync') {
            $this->page_name = 'user_sync';
        }
        if (strpos($path, 'setting/filmtype/') === 0 || $path == 'setting/filmtype') {
            $this->page_name = 'setting/filmtype';
        }
        if (strpos($path, 'setting/make/') === 0 || $path == 'setting/make') {
            $this->page_name = 'setting/make';
        }
        if (strpos($path, 'setting/model/') === 0 || $path == 'setting/model') {
            $this->page_name = 'setting/model';
        }
        if (strpos($path, 'setting/macro_management/') === 0 || $path == 'setting/macro_management') {
            $this->page_name = 'setting/macro_management';
        }
        if (strpos($path, 'setting/new-part/') === 0 || $path == 'setting/new-part') {
            $this->page_name = 'setting/new-part';
        }
        if (strpos($path, 'verify/precut_return/') === 0 || $path == 'verify/precut_return') {
            $this->page_name = 'precut_return';
        }
        if (strpos($path, 'verify/rolls_return/') === 0 || $path == 'verify/rolls_return') {
            $this->page_name = 'verify/rolls_return';
        }
        if (strpos($path, 'rolls/import/') === 0 || $path == 'rolls/import') {
            $this->page_name = 'rolls/import';
        }

        if (strpos($path, 'setting/part/') === 0 || $path == 'setting/part') {
            $this->page_name = 'setting/part';
        }

        if (strpos($path, 'newRoles/') === 0  || $path === 'newRoles') {
            $this->page_name = 'newRoles';
        }

        if (strpos($path, 'user/') === 0  || $path === 'user') {
            $this->page_name = 'user';
        }
        if (strpos($path, 'stock/') === 0  || $path === 'stock') {
            $this->page_name = 'stock';
        }

        if (strpos($path, 'warranty/import2') === 0  || $path === 'warranty/import2') {
            $this->page_name = 'warranty/import2';
        }

        if (strpos($path, 'warranty/asp_import') === 0  || $path === 'warranty/asp_import') {
            $this->page_name = 'warranty_asp_import';
        }

        if (strpos($path, 'warranty_delete') === 0  || $path === 'warranty_delete') {
            $this->page_name = 'warranty_delete';
        }

        if (strpos($path, 'stat/brand') === 0  || $path === 'stat/brand') {
            $this->page_name = 'stat_brand';
        }
        if (strpos($path, 'stat/roll') === 0  || $path === 'stat/roll') {
            $this->page_name = 'stat_roll';
        }
        if (strpos($path, 'stat/store') === 0  || $path === 'stat/store') {
            $this->page_name = 'stat_store';
        }
        if (strpos($path, 'stat/warranty') === 0  || $path === 'stat/warranty') {
            $this->page_name = 'stat_warranty';
        }
        if (strpos($path, 'stat/detail') === 0  || $path === 'stat/detail') {
            $this->page_name = 'stat_detail';
        }
        if (strpos($path, 'stat/price') === 0  || $path === 'stat/price') {
            $this->page_name = 'stat_price';
        }
    }

    /*
     * 省代权限*/
    protected function getDistributorMenu()
    {
        $admin_id = session('admin.id');
        $admin = Admin::find($admin_id);
        $menu = [
            [
                'icon' => 'home',
                'text' => ts('Dashboard'),
                'page_name' => ['index'],
            ],
        ];

        /*
         * 客户管理*/
        $customer = [
            'icon' => 'user-o',
            'text' => ts('MANAGECUSTOMER'),
            'url' => '#',
            'page_name' => ['customer', 'distributor', 'dealer'],
            'sub' => [
                [
                    'text' => ts('MANAGEDEALERS'),
                    'url' => 'dealer',
                    'page_name' => ['dealer'],
                ],
                [
                    'text' => ts('MANAGECAROWNERS'),
                    'url' => 'customer',
                    'page_name' => ['customer'],
                ],
            ]
        ];
        /*
         * 产品管理*/
        $product = [
            'icon' => 'home',
            'text' => ts('MANAGEPRODUCT'),
            'url' => '#',
            'page_name' => ['rolls'],
            'sub' => [
                [
                    'text' => ts('MANAGEroll'),
                    'url' => 'rolls',
                    'page_name' => ['rolls'],
                ],
            ]
        ];
       $new_daz = [
            'icon' => 'cogs',
            'text' => ts('DANTC'),
            'url' => 'new-precut/list',
            'page_name' => ['precut_deal'],
        ];
       /*
        * 质保管理*/
       $warranty = [
           'icon' => 'home',
           'text' => ts('MANAGEWARRANTY'),
           'url' => 'warranty',
           'page_name' => ['warranty'],
       ];
       /*
        * 专车专用套件*/
       $new_precut = [
           'icon' => 'cogs',
           'text' => ts('MANAGEPRecuT'),
           'page_name' => ['new-precut'],
           'url' => 'new-precut',
       ];
        /*
         * 卷调货*/
        $rolls_transfer = [
            'icon' => 'cogs',
            'text' => ts('DHGL'),
            'url' => '#',
            'page_name' => ['rolls_transfer', 'rolls-transfer'],
            'sub' => [
                [
                    'text' => ts('FAQIDIAOHUO'),
                    'url' => 'rolls-transfer/create',
                    'page_name' => ['rolls_transfer'],
                ],
                [
                    'text' => ts('WDDH'),
                    'url' => 'rolls-transfer',
                    'page_name' => ['rolls-transfer'],
                ],
            ]
        ];

        /*
         * 卷退货*/
        $rolls_return = [
            'icon' => 'cogs',
            'text' => ts('THGL'),
            'url' => '#',
            'page_name' => ['rolls_return', 'rolls-return'],
            'sub' => [
                [
                    'text' => ts('FQTH'),
                    'url' => 'rolls-return/create',
                    'page_name' => ['rolls_return'],
                ],
                [
                    'text' => ts('WDTH'),
                    'url' => 'rolls-return',
                    'page_name' => ['rolls-return'],
                ],
            ]
        ];

        $menu[] = $customer;
        $menu[] = $product;
        $menu[] = $warranty;
        if ($admin->new_precut == 1) {
            $menu[] = $new_precut;
        }
        $menu[] = $rolls_transfer;
        $menu[] = $rolls_return;

        return $this->makeUrlItem($menu);
    }

    /*
     * 管理员权限*/
    protected function getMenu()
    {
        $admin_id = session('admin.id');
        $admin = Admin::find($admin_id);
        $prefix = '/admin/';
        $menu = [
            [
                'icon' => 'home',
                'text' => ts('Dashboard'),
                'page_name' => ['index'],
            ],

            /*[
                'icon' => 'home',
                'text' => ts('bbzx'),
                'url' => 'charts',
                'page_name' => ['charts'],
            ],*/
        ];
        /*
         * 客户管理*/
        $customer = [
            'icon' => 'user-o',
            'text' => ts('MANAGECUSTOMER'),
            'url' => '#',
            'page_name' => ['customer', 'distributor', 'dealer'],
            'sub' => [
                [
                    'text' => ts('managedistributor'),
                    'url' => 'distributor',
                    'page_name' => ['distributor'],
                ],
                [
                    'text' => ts('MANAGEDEALERS'),
                    'url' => 'dealer',
                    'page_name' => ['dealer'],
                ],
                [
                    'text' => ts('MANAGECAROWNERS'),
                    'url' => 'customer',
                    'page_name' => ['customer'],
                ],
            ]
        ];
        /*
         * 产品管理*/
        $product = [
            'icon' => 'home',
            'text' => ts('MANAGEPRODUCT'),
            'url' => '#',
            'page_name' => ['rolls', 'rolls_transfer', 'rolls/import', 'rolls_return'],
            'sub' => [
                [
                    'text' => ts('MANAGEroll'),
                    'url' => 'rolls',
                    'page_name' => ['rolls'],
                ],
                [
                    'text' => ts('BULKIMPORT'),
                    'url' => 'rolls/import',
                    'page_name' => ['rolls/import'],
                ],
                [
                    'text' => ts('FAQIDIAOHUO'),
                    'url' => 'rolls-transfer/create',
                    'page_name' => ['rolls_transfer'],
                ],
                [
                    'text' => ts('FQTH'),
                    'url' => 'rolls-return/create',
                    'page_name' => ['rolls_return'],
                ],
            ]
        ];

        /*
         * 质保管理*/
        $warranty = [
            'icon' => 'home',
            'text' => ts('MANAGEWARRANTY'),
            'url' => 'warranty',
            'page_name' => ['warranty'],
        ];
        /*
         * 专车专用套件*/
        $new_precut = [
            'icon' => 'cogs',
            'text' => ts('MANAGEPRecuT'),
            'page_name' => ['new-precut'],
            'url' => 'new-precut',
        ];
        /*
         * 审核管理*/
        $verify = [
            'icon' => 'home',
            'text' => ts('EXAMINATIONMANAGEMENT'),
            'url' => '#',
            'page_name' => ['verify', 'verify_warranty', 'verify_transfer', 'verify/rolls_return', 'precut_return'],
            'sub' => [
                /*[
                    'text' => ts('VERIFYWARRANTY'),
                    'url' => 'verify/warranty',
                    'page_name' => ['verify_warranty'],
                ],*/
                [
                    'text' => ts('VERIFYtransfer'),
                    'url' => 'verify/transfer',
                    'page_name' => ['verify_transfer'],
                ],
                /*[
                    'text' => ts('TCTHGL'),
                    'url' => 'verify/precut_return',
                    'page_name' => ['precut_return'],
                ],*/
                [
                    'text' => ts('MJTHGL'),
                    'url' => 'verify/rolls_return',
                    'page_name' => ['verify/rolls_return'],
                ],
            ]
        ];
        /*
         * 监控中心*/
        $monitor = [
            'icon' => 'cogs',
            'text' => ts('MONITORINGCENTER'),
            'url' => '#',
            'page_name' => ['logs', 'dblogs', 'user_sync', 'distributor-o', 'dealer-o'],
            'sub' => [
                [
                    'text' => ts('OPERATIONRECORDS'),
                    'url' => 'logs',
                    'page_name' => ['logs'],
                ],
                [
                    'text' => ts('SJKCZ'),
                    'url' => 'dblogs',
                    'page_name' => ['dblogs'],
                ],
                /*[
                    'text' => ts('JXSTB'),
                    'url' => 'user_sync',
                    'page_name' => ['user_sync'],
                ],*/
                [
                    'text' => ts('CSSDGL'),
                    'url' => 'distributor-o',
                    'page_name' => ['distributor-o'],
                ],
                [
                    'text' => ts('CSJXSGL'),
                    'url' => 'dealer-o',
                    'page_name' => ['dealer-o'],
                ],
            ]
        ];
        /*
         * 数据字典*/
        $setting = [
            'icon' => 'cog',
            'text' => ts('DATADICTIONARY'),
            'url' => '#',
            'page_name' => ['cities', 'province', 'region', 'setting', 'setting/filmtype', 'setting/make', 'setting/model', 'setting/macro_management', 'setting/part', 'setting/new-part'],
            'sub' => [
                [
                    'text' => ts('CXBW'),
                    'url' => 'setting/part',
                    'page_name' => ['setting/part'],
                ],
                [
                    'text' => ts('FILMTYPE'),
                    'url' => 'setting/filmtype',
                    'page_name' => ['setting_filmtype', 'setting/filmtype'],
                ],
                [
                    'text' => ts('MAKE'),
                    'url' => 'setting/make',
                    'page_name' => ['setting_make', 'setting/make'],
                ],
                [
                    'text' => ts('MODEL'),
                    'url' => 'setting/model',
                    'page_name' => ['setting_model', 'setting/model'],
                ],
                [
                    'text' => ts('STATICTEXTMANAGEMENT'),
                    'url' => 'setting/macro_management',
                    'page_name' => ['setting_macro_management', 'setting/macro_management'],
                ],
                [
                    'text' => ts('TJPART'),
                    'url' => 'setting/new-part',
                    'page_name' => ['new-part', 'setting/new-part'],
                ],

            ]
        ];
        /*
         * 角色管理*/
        $roles = [
            'icon' => 'user',
            'text' => ts('jsgl'),
            'url' => 'roles',
            'page_name' => ['roles'],
        ];
        /*
         * 数据统计*/
        $sjtj = [
            'icon' => 'cogs',
            'text' => ts('SJTJ'),
            'url' => 'tables',
            'page_name' => ['tables'],
        ];

        $menu[] = $customer;
        $menu[] = $product;
        $menu[] = $warranty;
        if ($admin->new_precut == 1) {
            $menu[] = $new_precut;
        }
        $menu[] = $verify;
        $menu[] = $monitor;
        $menu[] = $setting;
        $menu[] = $roles;

        if (in_array($admin_id, [1,2])) {
            $menu[] = [
                'icon' => 'cogs',
                'text' => ts('MANAGEPRecuT'),
                'url' => 'precut',
                'page_name' => ['precut'],
            ];
        }

        /*[
            'icon' => 'cogs',
            'text' => ts('MANAGEPRecuT'),
            'url' => 'precut',
            'page_name' => ['precut'],
        ],*/

        return $this->makeUrlItem($menu);

    }

    protected function makeUrlItem($menu)
    {
        $prefix = '/admin/';
        $da =[];
        foreach ($menu as $k => $v) {
            if (!empty($v['level'])) {

            }
            if (!empty($v['url']) && $v['url'] == '#') {
                $v['url'] = 'javascript:void(0)';
            }else if (empty($v['external'])) {
                $v['url'] = $prefix . @$v['url'];
            } else {
                $v['url'] = @$v['url'];
            }
            if (!empty($v['sub'])) {
                foreach ($v['sub'] as $k2 => $v2) {
                    if (empty($v2['external'])) {
                        $v['sub'][$k2]['url'] = $prefix . @$v2['url'];
                    }
                }
            }
            $da[] = $v;
        }
        return $da;
    }


}
